﻿using Api.Core.Model.Module;
using System.Collections.Generic;
using System.Linq;

namespace Api.Core.Help
{
    public static class MenuEntityToDto
    {
        //public static List<MenuDto> GetTreeParent(List<MenuEntity> menuList)
        //{
        //    var dic = new Dictionary<int, MenuDto>(menuList.Count);
        //    foreach (var chapter in menuList)
        //    {
        //        dic.Add(chapter.Id, new MenuDto
        //        {
        //            Id = chapter.Id,
        //            Title = chapter.Title,
        //            Path = chapter.Path,
        //            ParentId = chapter.ParentId,
        //            Icon = chapter.Icon
        //        });
        //    }
        //    foreach (var chapter in dic.Values)
        //    {
        //        if (dic.ContainsKey(chapter.ParentId))
        //        {
        //            if (dic[chapter.ParentId].Children == null)
        //            {
        //                dic[chapter.ParentId].Children = new List<MenuDto>();
        //            }
        //            dic[chapter.ParentId].Children.Add(chapter);
        //        }
        //    }
        //    return dic.Values.Where(t => t.ParentId == 0).ToList();
        //}

        public static List<ModuleDto> GetTreeApi(List<ModuleEntity> menuList)
        {
            var dic = new Dictionary<int, ModuleDto>(menuList.Count);
            foreach (var chapter in menuList)
            {
                dic.Add(chapter.Id, new ModuleDto
                {
                    Id = chapter.Id,
                    Name = chapter.Name,
                    Path = chapter.Path,
                    Icon = chapter.Icon,
                    ParentId = chapter.ParentId,
                    Description = chapter.Description,
                    Enabled = chapter.Enabled,
                });
            }
            foreach (var chapter in dic.Values)
            {
                if (dic.ContainsKey(chapter.ParentId))
                {
                    if (dic[chapter.ParentId].Children == null)
                    {
                        dic[chapter.ParentId].Children = new List<ModuleDto>();
                    }
                    dic[chapter.ParentId].Children.Add(chapter);
                }
            }
            return dic.Values.Where(t => t.ParentId == 0).ToList();
        }


    }
}
